local goldLoot = GOLD_AMOUNT:gsub("%%d", "%(%%d+%)")
local silverLoot = SILVER_AMOUNT:gsub("%%d", "%(%%d+%)")
local copperLoot = COPPER_AMOUNT:gsub("%%d", "%(%%d+%)")

function guildLoot_OnLoad(self)
	self:Hide()
	self:RegisterEvent("PLAYER_ENTERING_WORLD")
	self:RegisterEvent("CHAT_MSG_MONEY")
	self:RegisterEvent("VARIABLES_LOADED")
end

function guildLoot_OnEvent(self, event, msg)
	if event == "PLAYER_ENTERING_WORLD" then
		guildLoot_currentTotal = guildLoot_currentTotal or 0
		print( GetCoinTextureString( guildLoot_currentTotal or 0 ) .. " since first running GuildLoot." )
		lootAmounts:SetText("Total Amount: " .. GetCoinTextureString( guildLoot_currentTotal or 0 ) )
		toonAmounts:SetText("Toon Total: " .. GetCoinTextureString( guildLoot_totalToon or 0 ) )
end
	if event == "VARIABLES_LOADED" then
		lootAmounts:SetText("Total Amount: " .. GetCoinTextureString( guildLoot_currentTotal ) )
		sessionAmounts:SetText("This Session: " .. GetCoinTextureString( 0 ) )
		guildLoot_Guild:SetText( "Your guild's bank." )
		toonAmounts:SetText("Toon Total: " .. GetCoinTextureString( guildLoot_totalToon or 0 ) )
	end
	if event == "CHAT_MSG_MONEY" then
		local _, guildMSG = msg:match( "(.+)%((.+)%)" )
		if guildMSG ~= nil then
			local gold = guildMSG:match(goldLoot) or 0
			local silver = 	guildMSG:match(silverLoot) or 0
			local copper = guildMSG:match(copperLoot) or 0
		
			if guildLoot_currentTotal == nil then
				guildLoot_currentTotal = 0
			else
				guildLoot_currentTotal = guildLoot_currentTotal
			end
				
			if guildLoot_SessionTotal == nil then
				guildLoot_SessionTotal = 0
			else
				guildLoot_SessionTotal = guildLoot_SessionTotal
			end

			if guildLoot_toonTotal == nil then
				guildLoot_toonTotal = 0
			else
				guildLoot_toonTotal = guildLoot_toonTotal
			end
	
			guildLoot_currentTotal = guildLoot_currentTotal + (gold*1000) + (silver*100) + copper
			guildLoot_SessionTotal = guildLoot_SessionTotal + (gold*1000) + (silver*100) + copper
			local guildLoot_mainTotal = guildLoot_currentTotal
			local guildLoot_yourGuild, _, _ = GetGuildInfo("player")
			guildLoot_totalToon = guildLoot_totalToon + (gold*1000) + (silver*100) + copper
	
			gold_Announce = gold
			silver_Announce = silver
			copper_Announce = copper

			guildLoot_Guild:SetText( guildLoot_yourGuild .. "'s bank." )
			lootAmounts:SetText("Total Amount: " .. GetCoinTextureString( guildLoot_mainTotal ) )
			sessionAmounts:SetText("This Session: " .. GetCoinTextureString( guildLoot_SessionTotal ) )
			toonAmounts:SetText("Toon Total: " .. GetCoinTextureString( guildLoot_totalToon ) )
			
			guildLoot_toonTotal = guildLoot_toonTotal + 1
       			guildLoot_currentTotal = guildLoot_currentTotal + 1
		end
	end
end

--guildLoot_Settings = {
--	MinimapPos = 0 -- default position of the minimap icon in degrees
--}

function guildLoot_MinimapButton_Reposition()
		guildLoot_MinimapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",52-(80*cos(guildLoot_Settings.MinimapPos)),(80*sin(guildLoot_Settings.MinimapPos))-52)
end


function guildLoot_MinimapButton_DraggingFrame_OnUpdate()
		local xpos,ypos = GetCursorPosition()
		local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
	
		xpos = xmin-xpos/UIParent:GetScale()+70
		ypos = ypos/UIParent:GetScale()-ymin-70

		guildLoot_Settings.MinimapPos = math.deg(math.atan2(ypos,xpos))
		guildLoot_MinimapButton_Reposition() -- move the button
end

function guildLoot_MinimapButton_OnClick(button, self)	
	if button == "LeftButton" then
		guildLoot:Show()
	else
		if guildLoot and guildLoot:IsShown() then
			guildLoot:Hide()
		elseif button == "LeftButton" and guildLoot:IsShown() then
			guildLoot:Hide()
		end
		
	end
end

function guildLoot_SendMoneyGuild_OnClick(button, self)
	local guildLoot_gold = gold_Announce; local guildLoot_silver = silver_Announce; local guildLoot_copper = copper_Announce; local guildLoot_give = guildLoot_currentTotal or 0 + guildLoot_gold + guildLoot_silver + guildLoot_copper; local yourGuild, _, _ = GetGuildInfo("player"); SendChatMessage ( GetCoinText( guildLoot_give, " / " ) .. " put in " .. yourGuild .. "'s bank, from me looting.", GUILD )
end

function guildLoot_SendMoneyParty_OnClick(button, self)
	local guildLoot_gold = gold_Announce; local guildLoot_silver = silver_Announce; local guildLoot_copper = copper_Announce; local guildLoot_give = guildLoot_currentTotal or 0 + guildLoot_gold + guildLoot_silver + guildLoot_copper; local yourGuild, _, _ = GetGuildInfo("player"); SendChatMessage ( GetCoinText( guildLoot_give, " / " ) .. " put in " .. yourGuild .. "'s bank, from me looting.", PARTY )
end

function guildLoot_Close_OnClick(button, self)
	guildLoot:Hide()
end